package com.zcsy.commons.mail;


import java.io.File;
import java.util.Map;

import javax.mail.internet.MimeMessage;

import org.apache.commons.lang.StringUtils;
import org.apache.velocity.app.VelocityEngine;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.ui.velocity.VelocityEngineUtils;

public class MailSenderService {
	private JavaMailSender mailSender;// spring配置中定义
	private VelocityEngine velocityEngine;// spring配置中定义
	private SimpleMailMessage simpleMailMessage;// spring配置中定义
	private static final String DELFAULT_ENCODING = "UTF-8";
	private String from;
	private String to;
	private String subject;
	private String content;
	private String templateName;
	private String baseUrl;
	private String fromCN;
	// 是否需要身份验证
	private boolean validate = false;

	public JavaMailSender getMailSender() {
		return mailSender;
	}

	public void setMailSender(JavaMailSender mailSender) {
		this.mailSender = mailSender;
	}

	public VelocityEngine getVelocityEngine() {
		return velocityEngine;
	}

	public void setVelocityEngine(VelocityEngine velocityEngine) {
		this.velocityEngine = velocityEngine;
	}

	public void sendTemplateEmail(final Map<String,Object> model,final String subject,final String vmfile,final String[] mailTo,final String [] files)
    {  
       MimeMessagePreparator preparator = new MimeMessagePreparator() {
           //注意MimeMessagePreparator接口只有这一个回调函数
         public void prepare(MimeMessage mimeMessage) throws Exception
         {
            MimeMessageHelper message = new MimeMessageHelper(mimeMessage,true,DELFAULT_ENCODING);
            //这是一个生成Mime邮件简单工具，如果不使用GBK这个，中文会出现乱码
            //如果您使用的都是英文，那么可以使用MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
            message.setTo(mailTo);//设置接收方的email地址
            message.setSubject(subject);//设置邮件主题
            message.setFrom(simpleMailMessage.getFrom(),fromCN);//设置发送方地址
            if(StringUtils.isNotEmpty(baseUrl)){
            	if(baseUrl.lastIndexOf("webapp")<0){
            		baseUrl = baseUrl + "/webapp";
            	}
            }
            model.put("baseUrl", baseUrl);
             String text = VelocityEngineUtils.mergeTemplateIntoString(
               velocityEngine, vmfile,DELFAULT_ENCODING, model);
             //从模板中加载要发送的内容，vmfile就是模板文件的名字
             //注意模板中有中文要加GBK，model中存放的是要替换模板中字段的值
            message.setText(text, true);
            //将发送的内容赋值给MimeMessageHelper,后面的true表示内容解析成html
            //如果您不想解析文本内容，可以使用false或者不添加这项
            FileSystemResource file;
            if(files!=null){
            	for(String s:files)//添加附件
                {
                   file = new FileSystemResource(new File(s));//读取附件
                   message.addAttachment(s, file);//向email中添加附件
                }
            }
          }
      };
 
      mailSender.send(preparator);//发送邮件
    }
	
	
	public void sendTextEmail(final String subject,final String text,final String[] mailTo,final String [] files)
    {  
       MimeMessagePreparator preparator = new MimeMessagePreparator() {
           //注意MimeMessagePreparator接口只有这一个回调函数
         public void prepare(MimeMessage mimeMessage) throws Exception
         {
            MimeMessageHelper message = new MimeMessageHelper(mimeMessage,true,DELFAULT_ENCODING);
            //这是一个生成Mime邮件简单工具，如果不使用GBK这个，中文会出现乱码
            //如果您使用的都是英文，那么可以使用MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
            message.setTo(mailTo);//设置接收方的email地址
            message.setSubject(subject);//设置邮件主题
            message.setFrom(simpleMailMessage.getFrom());//设置发送方地址
             //注意模板中有中文要加GBK，model中存放的是要替换模板中字段的值
            message.setText(text, true);
            //将发送的内容赋值给MimeMessageHelper,后面的true表示内容解析成html
            //如果您不想解析文本内容，可以使用false或者不添加这项
            FileSystemResource file;
            if(files!=null){
            	for(String s:files)//添加附件
                {
                   file = new FileSystemResource(new File(s));//读取附件
                   message.addAttachment(s, file);//向email中添加附件
                }
            }
          }
      };
 
      mailSender.send(preparator);//发送邮件
    }

	public String getFrom() {
		return from;
	}

	public void setFrom(String from) {
		this.from = from;
	}

	public String getSubject() {
		return subject;
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}

	public String getTo() {
		return to;
	}

	public void setTo(String to) {
		this.to = to;
	}

	public String getTemplateName() {
		return templateName;
	}

	public void setTemplateName(String templateName) {
		this.templateName = templateName;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public boolean isValidate() {
		return validate;
	}

	public void setValidate(boolean validate) {
		this.validate = validate;
	}

	public SimpleMailMessage getSimpleMailMessage() {
		return simpleMailMessage;
	}

	public void setSimpleMailMessage(SimpleMailMessage simpleMailMessage) {
		this.simpleMailMessage = simpleMailMessage;
	}

	public String getBaseUrl() {
		return baseUrl;
	}

	public void setBaseUrl(String baseUrl) {
		this.baseUrl = baseUrl;
	}

	public String getFromCN() {
		return fromCN;
	}

	public void setFromCN(String fromCN) {
		this.fromCN = fromCN;
	}
}